// pages/mine/second_page/purchase_history.js
var App = getApp();
var currentPage = 1;
var hasData = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    purchaseList:[],
    contentDis: false//缺省页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    currentPage = 1
    this.getMoneyList();
  },

  /**
   * 页面相关事件处理函数--监听用户上拉动作
   */
  onReachBottom: function () {
    if (hasData) {
      currentPage++;
      this.getMoneyList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //back
  backLastPage: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  //获取钱包消费列表
  getMoneyList:function(){
    let that = this;
    let max = 20;
    wx.request({
      url: App.data.globalAPI + '/users/wallet/log',
      data: {
        token: wx.getStorageSync('token'),
        page: currentPage,
        length: max
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          let list = res.data.data.list;
          var origin = that.data.purchaseList;
          if (list.length > 0 || origin.length>0){
            hasData = true;
            origin.push(...list);
            
            for (var i = 0; i < list.length; i++) {
              list[i].create_time = App.timeTransformat(list[i].create_time)
              if (list[i].type == 0) {//消费
                list[i].iconUrl = '../../../images/charge__history_logo.png';
                list[i].discribe = '消费';
                list[i].money = '-' + list[i].money;
              } else {
                list[i].iconUrl = '../../../images/charge__history_logo.png';
                list[i].discribe = '充值';
                list[i].money = '+' + list[i].money;
              }
            }
            that.setData({
              purchaseList: origin
            })
          }else{
            hasData = false;
            that.setData({
              contentDis:true
            })
          }
          if (list.length < max && !that.data.contentDis) {
            hasData = false;
            wx.showToast({
              title: '已加载全部数据...',
              icon: 'none',
              duration: 600
            })
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 600
          })
        }
      }
    })
  }
})